<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>销售单信息</title>
    <!-- 引入element-ui样式 -->
    <link rel="stylesheet" href="../../../plugins/element-ui/index.css">
</head>
<body>

<div id="app">

    <el-container style="height: 800px; border: 1px solid #eee">
        <el-container>
            <div style="width: 100%;position: relative">

                <div style=";margin-bottom: 80px;">
                    <el-input
                            placeholder="请输入商品名称"
                            clearable
                            style="width: 20%; "
                            prefix-icon="el-icon-search"
                            v-model="Query.name"
                    >
                    </el-input>
                    <el-button @click="getAll()" class="dalfBut">查询</el-button>

                    <el-button type="warning" style="float: right" @click="Up_Add_dialogShow(null)">新增销售单</el-button>
                </div>


                <el-table   size="medium" current-row-key="id"  :data="dataList" stripe highlight-current-row>
                    <el-table-column type="index" align="center" label="序号"></el-table-column>
                    <el-table-column prop="name" label="商品" align="center"></el-table-column>
                    <el-table-column prop="price" label="售价" align="center"></el-table-column>
<!--                    <el-table-column prop="job_number" label="员工工号" align="center"></el-table-column>-->
<!--                    <el-table-column prop="buyer_name" label="员工名" align="center"></el-table-column>-->
                    <el-table-column prop="number" label="数量" align="center"></el-table-column>
                    <el-table-column prop="total" label="总价" align="center"></el-table-column>
                    <el-table-column prop="time" label="销售时间" align="center"></el-table-column>

                    <el-table-column label="操作" align="center">
                        <template slot-scope="scope">
<!--                            <el-button type="primary"  size="mini" @click="Up_Add_dialogShow(scope.row)">编辑</el-button>-->
                            <el-button type="danger"  size="mini" @click="DeleteGoods(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--分页组件-->
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>


                <!--3.新增 销售单弹框-->
                <el-dialog :title="title" style="height: 650px"  :visible.sync="dialog_add">
                    <el-form :model="formData_add" :rules="rules" ref="formData_add" label-width="100px" class="demo-formData_add">
                        <el-form-item  label="商品：" prop="name">
                            <template>
                                <el-select v-model="formData_add.name"@change="selectedCag" placeholder="请选择">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.name"
                                            :label="item.label"
                                            :value="item.name">
                                    </el-option>
                                </el-select>
                            </template>
                        </el-form-item>


                        <el-form-item  label="售价：" prop="price">
                            <template>
                                <el-input-number v-model="formData_add.price"  :min="1" :max="1000" label="描述文字"></el-input-number>
                            </template>
                        </el-form-item>


                        <el-form-item  label="数量：" prop="number">
                            <template>
                                <el-input-number v-model="formData_add.number"  :min="1" :max="max" label="描述文字"></el-input-number>
                            </template>
                        </el-form-item>


                        <el-form-item>
                            <el-button type="primary" @click="AddGoods">确定添加</el-button>
                            <el-button @click="center">取消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>

            </div>
        </el-container>
    </el-container>

</div>


<script src="../../../plugins/vue/vue.js"></script>

<!-- 引入element-ui组件库 -->
<script src="../../../plugins/element-ui/index.js"></script>

<!-- 引入axios -->
<script src="../../../plugins/axios/axios.min.js"></script>

<script>
    new Vue({
        el:'#app',    //element
        data:{

            //显示的数据
            dataList:[],

            //销售单搜索内容1
            Query:{
                name:""
            },
            pagination: {
                //分页相关模型数据 1
                currentPage: 1,//当前页码1
                pageSize:5,//每页显示的记录数1
                total:0,//总记录数1
            },

            //修改弹框内容
            formData_update:{
                image:'',
            },
            dialog_update:false,

            //新增弹框内容
            formData_add:{
                image:'',
            },
            dialog_add:false,

            options:[{}],

            //最大商品数量
            max:'',
        },

        created() {
            const adminInfo = window.localStorage.getItem('salesman')
            console.log("adminInfo1:::"+adminInfo)
            if (adminInfo) {
                this.adminInfo = JSON.parse(adminInfo)
            }
            console.log(this.adminInfo)

            this.getAll();
        },

        methods:{
            getAll(){
                //将自己的jobNumber传过去 获取自己的销售单信息
                parent ="?name="+this.Query.name+"&jobNumber="+this.adminInfo.data.jobNumber;

                //显示当前登录销售单信息
                axios.get("/salesOrders/getPageSalesOrder/"+this.pagination.currentPage+"/"+this.pagination.pageSize+parent).then((res)=>{

                    console.log("res:")
                    console.log(res)

                    this.dataList=res.data.data.records
                    this.pagination.total=res.data.data.total
                    this.Query.name=""
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                console.log("切换页码:")
                this.pagination.currentPage=currentPage;
                console.log(currentPage)
                this.getAll();
            },

            selectedCag(row){
                console.log("row11")
                console.log(row)
                axios.get("/goodss/getGoodsNumberByName?name="+row).then((res)=> {
                    //根据选择的商品得到对应的库存数量
                    this.max=res.data.data.number
                })

            },

            //修改弹框的显示和隐藏
            Up_Add_dialogShow(row){

                axios.get("/goodss/getAll",this.formData_add).then((res)=> {
                    this.options=res.data.data
                })

                this.imageUrl = ""
                this.formData_add={}
                this.dialog_add=true

            },
            //确定添加
            AddGoods(){
                //封装数据
                //1.当前登录的个人信息
                this.formData_add.jobNumber =this.adminInfo.data.jobNumber
                this.formData_add.salesmanName =this.adminInfo.data.name
                //2.计算总价
                this.formData_add.total=this.formData_add.number*this.formData_add.price

                axios.post("/salesOrders/addSalesOrder",this.formData_add).then((res)=> {
                    if (res.data.code){
                        this.$message.success(res.data.msg)
                    }else{
                        this.$message.error(res.data.msg)
                    }
                }).finally(()=>{
                    this.getAll();
                    this.dialog_add=false;
                })
            },


            //通过id删除
            DeleteGoods(row){
                this.$confirm("此操作删除当销售单全部信息，是否继续?","提醒",{type: 'info'
                }).then(()=>{

                    axios.delete("/salesOrders/deleteSalesOrder/"+row.id).then((res)=> {

                        if (res.data.code){
                            this.$message.success(res.data.msg)
                        }else{
                            this.$message.error(res.data.msg)
                        }
                    }).finally(()=>{
                        this.getAll()
                    })

                }).catch(()=>{
                    //取消删除
                    this.$message.info("取消删除操作")
                })

            },

            center() {
                this.dialog_update=false
                this.dialog_add=false
                this.$message.info("取消操作")
                this.getAll()
            },


        }
    })

</script>
</body>
</html>